package com.example.administrator.yuejian_2.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.activity.PostDetailActivity;
import com.example.administrator.yuejian_2.bean.MyPostBean;
import com.example.administrator.yuejian_2.viewholder.MyPostVH;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/24.
 */

public class MyPostAdapter extends RecyclerView.Adapter {
    private List<MyPostBean> mypostlist = new ArrayList<>();
    private Context context;
    private LayoutInflater layoutInflater;

    public MyPostAdapter(Context context,List<MyPostBean> mypostlist){
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        this.mypostlist.addAll(mypostlist);

    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = layoutInflater.inflate(R.layout.item_mypost,parent,false);
        MyPostVH holder = new MyPostVH(itemView);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyPostVH myholder = (MyPostVH) holder;
        MyPostBean mypost = mypostlist.get(position);

        String uname = mypost.getU_name();
        myholder.username.setText(uname);

        String title = mypost.getTitle();
        myholder.posttitle.setText(title);

        String time = mypost.getTime();
        myholder.posttime.setText(time);

        String upic = mypost.getU_pic();
        if (!upic.equals(""))
            Picasso.with(context)
                    .load("http://10.0.2.2/YueJian16/Public/photo/"+upic)
                    .into(myholder.upic);

        String pic1 = mypost.getPic1();
        if (!pic1.equals(""))
            Picasso.with(context)
            .load("http://10.0.2.2/YueJian16/Public/photo/"+pic1)
            .into(myholder.postpic1);

        String pic2 = mypost.getPic2();
        if (!pic2.equals(""))
            Picasso.with(context)
                    .load("http://10.0.2.2/YueJian16/Public/photo/"+pic2)
                    .into(myholder.postpic2);

        String pic3 = mypost.getPic3();
        if (!pic3.equals(""))
            Picasso.with(context)
                    .load("http://10.0.2.2/YueJian16/Public/photo/"+pic3)
                    .into(myholder.postpic3);

        final int pid = mypost.getPic_id();
        myholder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, PostDetailActivity.class);
                intent.putExtra("pid",pid);
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mypostlist.size();
    }
}
